/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić              *
 *   ivasic@gmail.com                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "globals.h"
#include "torrentmanager.h"
#include "request.h"
#include "announcerequest.h"
#include "requesthandler.h"
#include "standardmessages.h"
#include "trackerdb.h"
#include "announcehandler.h"
#include "sha1hash.h"
#include "sha1hashgen.h"

#include "exceptions.h"

#include "bencoding.h"

#include <QTcpSocket>
#include <QtCore>

TorrentManager::TorrentManager(QObject *parent)
		: QObject(parent)
{}


TorrentManager::~TorrentManager()
{}

void TorrentManager::handleRequest(Request* request)
{
	try
	{
		switch (request->getType())
		{
				///@TODO: Handle request
			case Request::ANNOUNCE:
				handleAnnounceRequest(dynamic_cast<AnnounceRequest*>(request));
				break;
			case Request::SCRAPE:
				break;
			default :
				break;
		}
	}
	catch (Exception* ex)
	{
		throw ex;
	}
}

void TorrentManager::handleAnnounceRequest(AnnounceRequest* announce)
{
	if (torrentRegistered(announce->infohash()))
	{
		AnnounceHandler* h = Globals::instance().db()->createAnnounceHandler();
		h->handleRequest(announce);
		delete h;
	}
	else
	{
		//ok. I can handle this request - TORRENT NOT REGISTERED WITH THIS TRACKER!
		///@TODO: Swith public/private tracker behavior. I'm using private tr. behavior for now.

		StandardMessages::sendFailure("Torrent is not registered with this tracker", announce->socket());
	}
}

bool TorrentManager::torrentRegistered(const SHA1Hash& infohash) const
{
	return Globals::instance().db()->isTorrentRegistered(infohash);
}

void TorrentManager::registerTorrent(const QByteArray & metainfo, const QString & torrentName, const QString & description)
{
	Globals::instance().db()->registerTorrent(metainfo, torrentName, description);
}
